﻿using JustSiteWorkrole.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JustSiteWorker.Models
{
    public class CommentViewModel : Comment
    {
        public bool IsLeft { get; set; }
        public bool IsAnother { get; set; }

        public CommentViewModel()
        { }

        public CommentViewModel(Comment comment)
        {
            Id = comment.Id;
            UserId = comment.UserId;
            Message = comment.Message;
            Date = comment.Date;
            User = comment.User;
        }

        public static List<CommentViewModel> GetComments(Repository repos)
        {
            List<CommentViewModel> comments = new List<CommentViewModel>();
            CommentViewModel comment = new CommentViewModel();
            List<Comment> list = repos.Comments;


            for (int i = 0; i < list.Count; i++)
            {
                comment = new CommentViewModel(list[i]);

                if (i == 0)
                    comment.IsLeft = true;
                else
                    if (list[i - 1].UserId == list[i].UserId)
                    {
                        comment.IsLeft = comments[i - 1].IsLeft;
                        comment.IsAnother = false;
                    }
                    else
                    {
                        comment.IsLeft = !comments[i - 1].IsLeft;
                        comment.IsAnother = true;
                    }

                comments.Add(comment);
            }

            return comments;
        }
    }
}